package com.quick.calcu.controller;

import com.quick.calcu.entity.Student;
import com.quick.calcu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 根据ID查询学生
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getStudent(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Student student = studentService.getById(id);
            if (student != null) {
                result.put("code", "200");
                result.put("message", "查询成功");
                result.put("data", student);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "学生不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 根据班级ID查询学生列表
    @GetMapping("/class/{classId}")
    public ResponseEntity<Map<String, Object>> getStudentsByClass(@PathVariable Long classId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Student> students = studentService.getStudentsByClassId(classId);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", students);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 添加学生
    @PostMapping
    public ResponseEntity<Map<String, Object>> addStudent(@RequestBody Student student) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = studentService.save(student);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "添加成功" : "添加失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 更新学生信息
    @PutMapping
    public ResponseEntity<Map<String, Object>> updateStudent(@RequestBody Student student) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = studentService.updateById(student);
            result.put("success", success);
            result.put("code", "200");
            result.put("data", success);
            result.put("message", success ? "更新成功" : "更新失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    // 删除学生
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteStudent(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = studentService.removeById(id);
            result.put("code", "200");
            result.put("message", "查询成功");
            result.put("data", success);
            result.put("message", success ? "删除成功" : "删除失败");
            return success ? ResponseEntity.ok(result) : ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}